package com.github.shiqiyue.app.api.sapi.controller;


import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.config.captcha.ICaptchaService;
import com.github.shiqiyue.app.config.captcha.ICaptchaStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


/***
 * 图形验证码控制器
 *
 * @author wwy
 *
 */
@Slf4j
@Controller
@RequestMapping("/sapi/captcha")
public class SApiCaptchaController extends SApiBaseController {

    @Autowired
    private ICaptchaService captchaService;

    @Autowired
    private ICaptchaStore captchaStore;


    /***
     * 获取图形验证码
     * @param time
     * @param type
     * @param httpSession
     * @throws Exception
     */
    @PostMapping("get")
    @ResponseBody
    public Result handleRequest(@RequestParam(value = "time", required = false) Long time,
                                @RequestParam(value = "type", required = true) int type,
                                HttpSession httpSession) throws Exception {
        log.info(httpSession.getId());
        String text = captchaService.generateCaptcha();
        String base64Img = captchaService.generateBase64(text);
        captchaStore.store(text, type);
        return Result.success(base64Img);
    }
}
